
//
//  NewShopcartAddMinusView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/20.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  加减按钮
// 20 + 30 +20 =
#import "NewShopcartAddMinusView.h"

@implementation NewShopcartAddMinusView
#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void) setupView {
    [self addSubview:self.minusBtn];
    [self addSubview:self.countLb];
    [self addSubview:self.addBtn];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.minusBtn.mas_right);
        make.right.equalTo(self.addBtn.mas_left);
        make.centerY.equalTo(self.mas_centerY);
        make.height.equalTo(@20);
    }];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

#pragma mark - 二getter
- (UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:(UIControlStateNormal)]; //jia.png
        [_minusBtn addTarget:self action:@selector(clickMinusAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _minusBtn;
}
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_addBtn addTarget:self action:@selector(clickAddBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_addBtn setImage:[UIImage imageNamed:@"jia"] forState:(UIControlStateNormal)];
    }
    return _addBtn;
}

- (UILabel *)countLb {
    if (!_countLb) {
        _countLb = [UILabel new];
        _countLb.textAlignment = NSTextAlignmentCenter;
    }
    return _countLb;
}

#pragma mark - 三 layout
#pragma mark - 四 点击事件
- (void)clickMinusAction:(UIButton *)btn {
    
    //2019-2-15防止重复点击 https://www.jianshu.com/p/c2243ac4f620
    btn.enabled = NO; //禁用
    
    FDLog(@"点击了➖");
    if (self.minusBlock) {
        //回调当前数量
        self.minusBlock();
    }
    
    //启用
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        btn.enabled = YES;
    });
}

- (void)clickAddBtnAction:(UIButton *)btn {
    //防止重复点击
    btn.enabled = NO; //🈲
    
    
    if (self.addBlock) {
        //自+
        //更新数量
        //回调当前数量
        self.addBlock();
    }
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        btn.enabled = YES;  //启用
    });
}

@end
